package com.example.demojenkins.controller;

import com.example.demojenkins.entity.Menu;
import com.example.demojenkins.entity.Role;
import com.example.demojenkins.entity.TableData;
import com.example.demojenkins.service.RoleService;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * (Role)表控制层
 *
 * @author makejava
 * @since 2024-08-24 18:17:05
 */
@RestController
@RequestMapping("role")
public class RoleController {
    /**
     * 服务对象
     */
    @Resource
    private RoleService roleService;

    /**
     * 分页查询
     *
     * @param role 筛选条件
     * @param pageRequest      分页对象
     * @return 查询结果
     */
    @GetMapping
    public ResponseEntity<Page<Role>> queryByPage(Role role, PageRequest pageRequest) {
        return ResponseEntity.ok(this.roleService.queryByPage(role, pageRequest));
    }

    @PreAuthorize("hasAnyAuthority('text','admin')")
    @PostMapping(value = "/list")
    public Map<String, Object> queryByPage2(@RequestBody TableData tableData) {
        Integer page = tableData.getPageNum();
        Integer size = tableData.getPageSize();
        if (page != null) {
            page = (page - 1) * size;
        }
        Role role = new Role();
        Long count = roleService.count(role);
        List<Role> Roles = roleService.queryByPage2(page, size);
        Map<String, Object> map = new HashMap<>();
        map.put("total", count);
        map.put("data", Roles);
        return map;
    }

    @PreAuthorize("hasAnyAuthority('text','admin')")
    @GetMapping(value = "/findAll")
    public ResponseEntity<List<Role>> findAll() {
        return ResponseEntity.ok(roleService.findAll());
    }

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("{id}")
    public ResponseEntity<Role> queryById(@PathVariable("id") Integer id) {
        return ResponseEntity.ok(this.roleService.queryById(id));
    }

    /**
     * 新增数据
     *
     * @param
     * @return 新增结果
     */
    @PreAuthorize("hasAnyAuthority('text','admin')")
    @PostMapping("/save")
    public ResponseEntity<Role> add(@RequestBody Role role) {
        return ResponseEntity.ok(this.roleService.insert(role));
    }

    /**
     * 编辑数据
     *
     * @param role 实体
     * @return 编辑结果
     */
    @PreAuthorize("hasAnyAuthority('text','admin')")
    @PostMapping("/update")
    public ResponseEntity<Role> edit(@RequestBody Role role) {
        return ResponseEntity.ok(this.roleService.update(role));
    }

    /**
     * 删除数据
     *
     * @param
     * @return 删除是否成功
     */
    @PreAuthorize("hasAnyAuthority('text','admin')")
    @PostMapping("/delete")
    public ResponseEntity<Boolean> deleteById(@RequestBody Role role) {
        return ResponseEntity.ok(this.roleService.deleteById(role.getRoleId()));
    }

}

